package org.dding.mvc.web.tag;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.dding.core.util.StringUtils;

public class ListTag extends SimpleTagSupport {

	private String value;

	private String split;

	public String getSplit() {
		return split;
	}

	public void setSplit(String split) {
		this.split = split;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}

	private String var;

	@Override
	public void doTag() throws JspException {

		ServletRequest request = ((PageContext) getJspContext()).getRequest();
		List<String> result = new ArrayList<String>();

		if (!StringUtils.isEmpty(value)) {

			if (!StringUtils.isEmpty(split)) {
				result.add(value);
			} else {

				StringTokenizer token = new StringTokenizer(value, split);
				while (token.hasMoreTokens()) {
					result.add(token.nextToken());
				}
			}
		}
		request.setAttribute(var, result);
	}

}
